import os
import shutil
from easydict import EasyDict
import yaml
import re
import argparse

def mk_path(path, remove=False):
    try:
        if not os.path.exists(path):
            os.makedirs(path)
        else:
            if remove:
                shutil.rmtree(path, ignore_errors=True)
    except Exception as e:
        print(e)


def loadyaml(file_path):
    if file_path is not None:
        try:
            with open(file_path, "r", encoding="utf-8") as f:
                return EasyDict(yaml.load(f, Loader=yaml.FullLoader))
        except IOError as e:
            print(e)
    else:
        print("文件路径为空")
        return None

class Namespace(object):
    """
    load the configs from xxxx.yaml
    args.key
    """

    def __init__(self, dic):
        for key, value in dic.items():
            assert isinstance(key, str) and re.match("[A-Za-z_-]", key)
            if isinstance(value, dict):
                self.__dict__[key] = Namespace(value)
            else:
                self.__dict__[key] = value

    def __getattr__(self, attribute):
        raise AttributeError(f"Can not find {attribute} in namespace. Please write {attribute} in config file!")


def load_yaml(path):
     parser = argparse.ArgumentParser()
     args = parser.parse_args()
     with open(path, 'r') as f:
        for key, value in Namespace(yaml.load(f, Loader=yaml.FullLoader)).__dict__.items():
            vars(args)[key] = value

# if __name__ == '__main__':
#     # 明确指定日志输出的文件路径和日志级别
#     logger = _get_logger('./logs/test.log', 'info')
#     logger.info('日志输出测试')
